/**
 * Appointment
 * This class represents an appointment
 */
package docoffice.doss;
import java.util.ArrayList;
import java.util.logging.Logger;
import java.sql.ResultSet;

import docoffice.database.DatabaseManager;
/**
 * @author George Chi
 * @version $Revision: 1.8 $
 */
public class Appointment {
	
	/**
	 * Field date.
	 */
	public String date; // date of appointment, can be written as "mm/dd/yy"

	/**
	 * Field time.
	 */
	public String time; // time of appointment, can be written as "hh:mm"

	/**
	 * Field reason.
	 */
	public String reason; // reason for appointment, written as sentence fragments

	/**
	 * Field desiredDoc.
	 */
	public Doctor desiredDoc; // choose a doctor from a list of Doctors

	/**
	 * Field treatment.
	 */
	public TreatmentRec treatment; // creates new treatmentRec
	
	/**
	 * Field patient.
	 */
	private Patient patient;
	
	/**
	 * Field id.
	 */
	private int id;
		
	/**
	 * Field LOG.
	 */
	private static final Logger LOG = Logger.getLogger("Appointment");
	
	/**
	 * Constructor for Appointment.
	 * @param inputDate String
	 * @param inputTime String
	 * @param inputReason String
	 * @param inputDoc Doctor
	 */
	public Appointment(String inputDate, String inputTime, 
			String inputReason, Doctor inputDoc){
		setDate(inputDate);
		setTime(inputTime);
		setReason(inputReason);
		setDesiredDoc(inputDoc);
	}

	/**
	 * Method getDate.
	
	 * @return String  */
	public String getDate() {
		return date;
	}

	/**
	 * Method setDate.
	 * @param date String
	 */
	public final void setDate(String date) {
		this.date = date;
	}

	/**
	 * Method getTime.
	
	 * @return String */
	public String getTime() {
		return time;
	}

	/**
	 * Method setTime.
	 * @param time String
	 */
	public final void setTime(String time) {
		this.time = time;
	}

	/**
	 * Method getReason.
	
	 * @return String */
	public String getReason() {
		return reason;
	}

	/**
	 * Method setReason.
	 * @param reason String
	 */
	public final void setReason(String reason) {
		this.reason = reason;
	}

	/**
	 * Method getDesiredDoc.
	
	 * @return Doctor */
	public Doctor getDesiredDoc() {
		return desiredDoc;
	}

	/**
	 * Method setDesiredDoc.
	 * @param desiredDoc Doctor
	 */
	public final void setDesiredDoc(Doctor desiredDoc) {
		this.desiredDoc = desiredDoc;
	}

	/**
	 * Method getTreatment.
	
	 * @return TreatmentRec */
	public TreatmentRec getTreatment() {
		return treatment;
	}

	/**
	 * Method setTreatment.
	 * @param treatment TreatmentRec
	 */
	public final void setTreatment(TreatmentRec treatment) {
		this.treatment = treatment;
	}
	
	/**
	 * id getter
	
	 * @return id */
	public int getId(){
		return this.id;
	}
	
	/**
	 * id setter
	 * @param id
	 */
	public void setId(int id){
		this.id = id;
	}
	
	public Patient getPatient(){
		return this.patient;
	}
	
	public void setPatient(Patient patient){
		this.patient = patient;
	}
	
	/**
	 * Method toString.
	
	 * @return String  */
	public String toString() {
		return "Appointment will return a patient's appointment information.";
	}
	
	/**
	 * Method createAppointment.
	 * @param patient Patient
	 * @param date String
	 * @param time String
	 * @param reason String
	 * @param doc Doctor
	 * @param treatment TreatmentRec
	 */
	public void createAppointment(String date, String time, 
			String reason, Doctor doc, Patient patient, TreatmentRec treatment) {
		final int docID = doc.getId();
		final int patientID = patient.getId();
		final int treatmentID = treatment.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "INSERT INTO appointments (PatientID, TreatmentRecID," +
				" RequestedDocID, AppointmentDate, AppointmentTime, Reason) VALUES (" + 
				patientID + ", " + treatmentID + ", " + 
				docID + ", '" + date + "', '" + time + "', '" + reason + "')";
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while creating appointment");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Appointment successfully created");
	}
	
	/**
	 * Method readAppointment.
	 * @param appt Appointment
	
	 * @return Appointment */
	public Appointment readAppointment(Appointment appt) {
		final int id = appt.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT * FROM appointments WHERE AppointmentID = " + id;
		ResultSet rs = null;
		
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			final int patientID = rs.getInt("PatientID");
			final int treatmentID = rs.getInt("TreatmentRecID");
			final int docID = rs.getInt("RequestedDocID");
			final String date = rs.getString("AppointmentDate");
			final String time = rs.getString("AppointmentTime");
			final String reason = rs.getString("Reason");
			
			Patient patient = new Patient("");
			patient.setId(patientID);
			patient = patient.readPatient();
			TreatmentRec treatment = new TreatmentRec(null, "", "", "", "", 
					null, null, null);
			treatment.setId(treatmentID);
			treatment = treatment.readTreatmentRec(treatment);
			Doctor doc = new Doctor("", "");
			doc.setId(docID);
			doc = doc.readDoctor();
			
			appt.setDate(date);
			appt.setDesiredDoc(doc);
			appt.setReason(reason);
			appt.setTime(time);
			appt.setTreatment(treatment);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while reading appointment");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("Appointment read successfully");
		return appt;
	}
	
	/**
	 * Method updateAppointment.
	 * @param appt Appointment
	 */
	public void updateAppointment(Appointment appt) {
		final int id = appt.getId();
		final int treatmentID = appt.treatment.getId();
		final int docID = appt.desiredDoc.getId();
		final String date = appt.getDate();
		final String time = appt.getTime();
		final String reason = appt.getReason();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "UPDATE appointments SET TreatmentRecID = " + 
		        treatmentID + ", RequestedDocID = " + docID + ", AppointmentDate = " 
		        + date + ", AppointmentTime = " + time + ", Reason = " + reason + 
		        " WHERE AppointmentID = " + id;
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while updating appointment info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Appointment successfully updated");
	}
	
	/**
	 * Method deleteAppointment.
	 * @param appt Appointment
	 */
	public void deleteAppointment(Appointment appt) {
		final int id = appt.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "DELETE FROM appointments WHERE AppointmentID = " + id;
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while deleting appointment");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Appointment deleted successfully");
	}

	/**
	 * Method readAppointmentId.
	 * @param appt Appointment
	 * @return int
	 */
	public int readAppointmentId(Appointment appt){
		int id = 0;
		final int treatmentID = appt.treatment.readTreatmentRecId(treatment);
		System.out.println(treatmentID);
		final int docID = appt.desiredDoc.readMedicalEmployeeID();
		final String date = appt.getDate();
		final String time = appt.getTime();
		final String reason = appt.getReason();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT AppointmentID FROM appointments WHERE" +
				" TreatmentRecID = " + treatmentID + " AND RequestedDocID = " + docID 
				+ " AND AppointmentDate = '" + date + "' AND AppointmentTime = '" + time
				+ "' AND Reason = '" + reason + "'";
		ResultSet rs = null;
		System.out.println(sqlQuery);
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			if (rs.first()){
				id = rs.getInt("AppointmentID");
			}
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while reading appointment id");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("Appointment id read successfully");
		return id;
	}
	
	/**
	 * Method readAllAppointments
	 */
	public ArrayList<String> readAllAppointments(){
		ArrayList<String> appointments = new ArrayList<String>();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT PatientName, AppointmentDate, AppointmentTime, Reason " +
				"FROM appointments a INNER JOIN patient p ON  a.PatientID = p.PatientID";
		ResultSet rs = null;
		System.out.println(sqlQuery);
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			while(rs.next()){
				String name = rs.getString("PatientName");
				String date = rs.getString("AppointmentDate");
				String time = rs.getString("AppointmentTime");
				String reason = rs.getString("Reason");
				String appointment = "Patient: " + name + ", Date: " + 
				        date + ", Time: " + time + ", Reason: " + reason;
				appointments.add(appointment);
			}
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while reading appointment id");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("Appointment id read successfully");
		return appointments;
	}
}
